<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="机构名" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入机构名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="所在城市" prop="originPlace">
        <provice-select
          ref="provice-select"
          @changeValue="queryChangeValue"
          :isShowDistrict="true"
        ></provice-select>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="wbEntAuthList">
      <el-table-column label="用户名称" align="center" prop="responsibleName" />
      <el-table-column label="电话" align="center" prop="responsiblePhone" />
      <el-table-column label="机构名" align="center" prop="companyName" />
      <el-table-column label="状态" align="center" prop="auditType">
        <template slot-scope="{ row }">
          <div v-if="row.auditType == 1" style="color: #e6a23c">
            {{ forrmatterEnmu(statusList, row.auditType) }}
          </div>
          <div v-if="row.auditType == 2" style="color: #67c23a">
            {{ forrmatterEnmu(statusList, row.auditType) }}
          </div>
          <div v-if="row.auditType == 3" style="color: #f56c6c">
            {{ forrmatterEnmu(statusList, row.auditType) }}
          </div>
        </template>
      </el-table-column>

      <el-table-column label="申请时间" align="center" prop="createTime" />

      <el-table-column
        label="审核时间"
        align="center"
        prop="updateBy"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.updateBy }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="审核人"
        align="center"
        prop="updateTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="detailHandler(scope.row)"
            >查看</el-button
          >

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditType == 1"
            @click="by(scope.row)"
            v-hasPermi="['wb:wbEntAuth:pass']"
            >通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.auditType == 1"
            @click="handleAudit(scope.row, 1)"
            v-hasPermi="['wb:wbEntAuth:edit']"
            >驳回</el-button
          >
          <!-- <el-button size="mini" type="text" @click="delHandler(scope.row)"
            v-hasPermi="['wb:wbEngineer:reject']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form v-if="optionType == 1">
        <el-form-item label="驳回原因" prop="entName" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :inline="true"
        class="dialog-form"
        v-else
      >
        <el-form-item label="机构名" prop="entName" class="form-item-row">
          <el-input
            v-model="form.entName"
            placeholder="请输入机构名"
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人/法人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所在地址:">
          <provice-select
            :isShowDistrict="true"
            :defaultCity="defaultCity"
            @changeValue="changeValue"
          ></provice-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="entAddress" class="form-item-row">
          <el-input
            v-model="form.entAddress"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="描述" prop="remark" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          label="营业执照"
          prop="businessLicense"
          class="form-item-row"
        >
          <image-upload v-model="form.businessLicense" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button v-if="optionType == 1" type="primary" @click="submitForm"
          >驳回</el-button
        >
        <el-button v-else type="primary" @click="by">审核通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改维保公司对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open1"
      width="900px"
      append-to-body
    >
      <el-form
        ref="form1"
        :model="form1"
        :rules="rules1"
        label-width="110px"
        :inline="true"
        class="dialog-form"
      >
        <el-form-item label="公司名称" prop="companyName" class="form-item-row">
          <el-input v-model="form1.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form1.userName" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form1.password"
            show-password
            placeholder="请输入密码"
          />
        </el-form-item>

        <el-form-item label="联系人/法人" prop="contact">
          <el-input v-model="form1.contact" placeholder="请输入联系人/法人" />
        </el-form-item>
        <el-form-item label="法人身份证号" prop="idCard">
          <el-input v-model="form1.idCard" placeholder="请输入法人身份证号" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phoneNumber">
          <el-input v-model="form1.phoneNumber" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="公司简介" prop="intro" class="form-item-row">
          <el-input
            v-model="form1.intro"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="所在地址:">
          <provice-select
            :isShowDistrict="true"
            :defaultCity="defaultCity"
            @changeValue="changeValue"
          ></provice-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address" class="form-item-row">
          <el-input v-model="form1.address" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="描述" prop="remark" class="form-item-row">
          <el-input
            v-model="form1.remark"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入描述"
          />
        </el-form-item>
        <el-form-item
          label="法人身份证正面"
          prop="idCardPicA"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form1.idCardPicA" :limit="1" />
        </el-form-item>
        <el-form-item
          label="法人身份证反面"
          prop="idCardPicB"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form1.idCardPicB" :limit="1" />
        </el-form-item>
        <el-form-item
          label="营业执照"
          prop="businessLicense"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form1.businessLicense" :limit="1" />
        </el-form-item>

        <el-form-item
          label="公司logo"
          prop="logo"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form1.logo" :limit="1" />
        </el-form-item>

        <el-form-item
          label="公章"
          prop="commonSeal"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form1.commonSeal" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="open1 = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提示" :visible.sync="open2" width="400px" append-to-body>
      <el-form>
        <el-form-item
          label="驳回原因"
          prop="causeOfRejection"
          class="form-item-row"
        >
          <el-input
            v-model="rejectedObj.causeOfRejection"
            type="textarea"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejected">确定</el-button>
        <el-button @click="open2 = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 维修师傅详情 -->
    <el-dialog title="详情" :visible.sync="isShow" width="800px" append-to-body>
      <el-descriptions :column="2">
        <el-descriptions-item label="用户名称">{{
          detailForm.responsibleName
        }}</el-descriptions-item>
        <el-descriptions-item label="联系方式">{{
          detailForm.responsiblePhone
        }}</el-descriptions-item>
        <el-descriptions-item label="机构名称">{{
          detailForm.companyName
        }}</el-descriptions-item>
        <el-descriptions-item label="审核状态">
          {{ forrmatterEnmu(statusList, detailForm.auditType) }}
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{
          detailForm.createTime
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWbEntAuth,
  getWbEntAuth,
  delWbEntAuth,
  addWbEntAuth,
  updateWbEntAuth,
} from "@/api/wb/wbEntAuth";
import {
  organizationList,
  organizationCheck,
  delOrganization,
} from "@/api/wb/wbEntAuth";

import { addWbMaintainCompany } from "@/api/wb/wbMaintainCompany";
import { mapGetters } from "vuex";

import ProviceSelect from "@/components/ProviceSelect/index.vue";
import { fomatterAdressName, forrmatterEnmu } from "@/utils/index.js";
export default {
  name: "WbEntAuth",
  components: { ProviceSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构认证表格数据
      wbEntAuthList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      isShow: false,
      rejectedObj: {
        id: "",
        causeOfRejection: "",
        auditType: 3,
        ifRead: 1,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entName: null,
        entType: null,
        contact: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      rules1: {
        phoneNumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        idCard: [
          { required: true, message: "身份证号为空", trigger: "blur" },
          {
            pattern:
              /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
            message: "请输入正确的身份证号",
            trigger: "blur",
          },
        ],
        // contact
        contact: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "用户密码长度必须介于 5 和 20 之间",
            trigger: "blur",
          },
          {
            pattern: /^[^<>"'|\\]+$/,
            message: "不能包含非法字符：< > \" ' \\\ |",
            trigger: "blur",
          },
        ],
      },
      detailForm: {},
      statusList: [
        {
          value: 1,
          name: "待审核",
        },
        {
          value: 2,
          name: "已通过",
        },
        {
          value: 3,
          name: "已驳回",
        },
      ],
      defaultCity: {
        province: "",
        city: "",
        county: "",
        cityString: "",
        countyString: "",
        provinceString: "",
      },
      optionType: 0,
      open1: false,
      id: "",
      // 表单参数
      form1: {},
    };
  },
  computed: {
    ...mapGetters(["allCityList", "provincesList", "cityslist", "regionslist"]),
  },
  created() {
    this.getList();
  },
  methods: {
    fomatterAdressName,
    forrmatterEnmu,
    /** 查询机构认证列表 */
    getList() {
      this.loading = true;
      console.log("888", this.queryParams);
      organizationList(this.queryParams).then((response) => {
        this.wbEntAuthList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entName: null,
        entAddress: null,
        entType: null,
        businessLicense: null,
        contact: null,
        phoneNumber: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
      };
      this.resetForm("form");
    },

    by(row) {
      this.form1 = {
        companyName: row.companyName,
        password: "123456",
        // userName: row.responsibleName,
        // contact: row.responsibleName,
        phoneNumber: row.responsiblePhone,
      };

      this.resetForm("form1");
      this.id = row.id;
      this.open1 = true;
    },

    /**查看详情 */
    detailHandler(row) {
      this.isShow = !this.isShow;
      this.detailForm = row;
    },

    /** 提交按钮 */
    submitForm1() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          organizationCheck({
            id: this.id,
            auditType: 2,
            wbMaintainCompanyDto: this.form1,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success("审核成功");
              this.open1 = false;
              this.getList();
            }
          });
        }
      });
    },

    changeValue(values) {
      this.form1.province = values[0];
      this.form1.city = values[1];
      this.form1.county = values[2];

      this.provincesList.forEach((element) => {
        if (element.id == this.form1.province) {
          this.form1.provinceString = element.cname;
        }
      });

      this.cityslist.forEach((element) => {
        if (element.id == this.form1.city) {
          this.form1.cityString = element.cname;
        }
      });

      this.allCityList.forEach((element) => {
        if (element.id == this.form1.county) {
          this.form1.countyString = element.cname;
        }
      });
    },
    queryChangeValue(values) {
      console.log(values);
      this.queryParams.province = values[0];
      this.queryParams.city = values[1];
      this.queryParams.county = values[2];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.province = null;
      this.queryParams.city = null;
      this.queryParams.county = null;
      this.$refs["provice-select"].province = "";
      this.$refs["provice-select"].city = "";
      this.$refs["provice-select"].district = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机构认证";
    },
    handleAudit(row, flag) {
      this.open2 = true;
      this.rejectedObj.id = row.id;
      this.rejectedObj.causeOfRejection = "";
      return;
      organizationCheck({ id: row.id, auditType: 3 }).then((res) => {
        if (res.code == 200) {
          this.$message.success("审核成功");
          this.getList();
        }
      });
    },

    rejected() {
      organizationCheck(this.rejectedObj).then((res) => {
        if (res.code == 200) {
          this.open2 = false;
          this.$message.success("审核成功");
          this.getList();
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            let params = {};
            if (this.title == "审核") {
              params = {
                ...this.form,
                state: 1,
              };
            } else {
              params = {
                ...this.form,
              };
            }
            console.log(params);
            updateWbEntAuth(params).then((response) => {
              this.$modal.msgSuccess(
                this.title == "审核" ? "审核成功" : "修改成功"
              );
              this.open = false;
              this.getList();
            });
          } else {
            addWbEntAuth(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbEntAuth/export",
        {
          ...this.queryParams,
        },
        `wbEntAuth_${new Date().getTime()}.xlsx`
      );
    },
    delHandler(row) {
      delOrganization(row.id).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.getList();
        }
      });
    },
  },
};
</script>
